//
//  TabBarViewController.m
//  quanminzhekou
//
//  Created by zxl on 15/1/6.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "TabBarViewController.h"

@interface TabBarViewController ()

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViewControllers];
    
    [self tabBarTapHandler];
    
    [self.view bringSubviewToFront:self.tabBarBg];
    [self.view bringSubviewToFront:self.tabBar];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //因为当前controller是APP的Inital View Controller，而且与其他子controller没有任何segue关系，必须等待当前界面加载完全后，再设置子controller的显示！否则适配所有屏幕size无效！
    [self.tabBar defulteStatus];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
    _isTabBarHidden = hidden;
    
    __weak typeof(self) weakSelf = self;
    void (^hiddenBlock)() = ^{
        if (hidden) {
            weakSelf.tabBar.transform = CGAffineTransformMakeTranslation(0, 49);
            weakSelf.tabBarBg.transform = CGAffineTransformMakeTranslation(0, 49);
        } else {
            weakSelf.tabBar.hidden = NO;
            weakSelf.tabBar.transform = CGAffineTransformIdentity;
            
            weakSelf.tabBarBg.hidden = NO;
            weakSelf.tabBarBg.transform = CGAffineTransformIdentity;
        }
    };
    
    void (^completion)(BOOL) = ^(BOOL finished){
        if (hidden) {
            weakSelf.tabBar.hidden = YES;
            weakSelf.tabBarBg.hidden = YES;
        }
    };
    
    if (animated) {
        [UIView animateWithDuration:0.24 animations:hiddenBlock completion:completion];
    } else {
        hiddenBlock();
        completion(YES);
    }
}

#pragma mark -
- (void)setupViewControllers {
    _homeController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeNaviViewController"];
    _freeController = [self.storyboard instantiateViewControllerWithIdentifier:@"FreePostageNaviViewController"];
    _recommendController = [self.storyboard instantiateViewControllerWithIdentifier:@"RecommendNaviViewController"];
    _tomorrowController = [self.storyboard instantiateViewControllerWithIdentifier:@"TomorrowNaviViewController"];
}

- (void)setupSelectedController:(UIViewController*)controller {
    if (self.selectedController && [self.selectedController isEqual:controller]) {
        return;
    }
    
    if (self.selectedController) {
        [self.selectedController willMoveToParentViewController:nil];
        [self.selectedController.view removeFromSuperview];
        [self.selectedController removeFromParentViewController];
    }
    
    self.selectedController = controller;
    
    [self addChildViewController:self.selectedController];
    [self.contentView addSubview:self.selectedController.view];
    
}

- (void)tabBarTapHandler {
    __weak typeof(self) weakSelf = self;
    
    self.tabBar.tapFirstBtnHandler = ^{
        [weakSelf setupSelectedController:_homeController];
    };
    
    self.tabBar.tapSecondBtnHandler = ^{
        [weakSelf setupSelectedController:_freeController];
    };
    
    self.tabBar.tapThirdBtnHandler = ^{
        [weakSelf setupSelectedController:_recommendController];
    };
    
    self.tabBar.tapFourthBtnHandler = ^{
        [weakSelf setupSelectedController:_tomorrowController];
    };
}

@end
